package be.fedict.barth.signer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CRLException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.util.Calendar;
import javax.naming.InvalidNameException;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDPropBuild;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDPropBuildDataDict;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.bouncycastle.cert.ocsp.OCSPException;
import org.bouncycastle.operator.OperatorCreationException;

/**
 * Hello world!
 *
 */
public class App 
{ 
    private static PDSignature prepareSignature(String name) {
        
        PDPropBuildDataDict app = new PDPropBuildDataDict();
        app.setName("PAdES demo");
        PDPropBuild prop = new PDPropBuild();
        prop.setPDPropBuildApp(app);
        
        PDSignature pdfsig = new PDSignature();
        pdfsig.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
        pdfsig.setSubFilter(PDSignature.SUBFILTER_ETSI_CADES_DETACHED);
        pdfsig.setName(name);
        pdfsig.setSignDate(Calendar.getInstance());
        pdfsig.setPropBuild(prop);
        return pdfsig;
    }
    
    private static PDDocument loadPDF(String file) throws IOException {
        return PDDocument.load(new File(file));
    }
    
    public static void main( String[] args ) throws CertificateEncodingException {
        try {
            PDDocument pdf = loadPDF("b:\\test.pdf");
            PadesSigner signer = new PadesSigner();
            
            String name = signer.getKeys();
            signer.getRevocation();

            FileInputStream fis = new FileInputStream("b:\\test.pdf");
            FileOutputStream fos = new FileOutputStream("b:\\test-signed.pdf");
            IOUtils.copy(fis, fos);
            
            fis = new FileInputStream("b:\\test-signed.pdf");
            
            PDSignature pdsig = prepareSignature(name);
            pdf.addSignature(pdsig, signer);
            signer.addDSS(pdf);
            
            pdf.saveIncremental(fis, fos);
        } catch (IOException ex) {
            System.err.println(ex.getMessage());      
        } catch (KeyStoreException ex) {
            System.err.println(ex.getMessage());
        } catch (NoSuchAlgorithmException ex) {
            System.err.println(ex.getMessage());
        } catch (CertificateException ex) {
            System.err.println(ex.getMessage());
        } catch (UnrecoverableEntryException ex) {
            System.err.println(ex.getMessage());
        } catch (InvalidNameException ex) {
            System.err.println(ex.getMessage());
        } catch (SignatureException ex) {
            System.err.println(ex.getMessage());
        } catch (COSVisitorException ex) {
            System.err.println(ex.getMessage());
        } catch (OCSPException ex) {
            System.err.println(ex.getMessage());
        } catch (CRLException ex) {
            System.err.println(ex.getMessage());
        } catch (NoSuchProviderException ex) {
            System.err.println(ex.getMessage());
        } catch (OperatorCreationException ex) {
            System.err.println(ex.getMessage());
        }

    }
}
